package com.halflife.framework.generator.template.simple;

import com.halflife.framework.generator.GenerateUtil;
import com.halflife.framework.generator.annotation.QueryAnn;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.dom4j.Element;

import java.io.*;
import java.lang.reflect.Field;
import java.util.*;

/**
 * Created by zhaoyf on 2018/8/25.
 */
public abstract class BaseAnalysis {
    private Configuration configuration = null;
    protected String history;
    protected String current;
    private String packageName;
    private String basePackagePath;
    private String templateFile;
    protected Element element;
    protected StringBuilder xmlStr;
    protected Class clazz;
    protected String appLocation;
    protected String srcLocation;
    protected String webLocation;
    Map<String,Object> dataMap=new HashMap<String,Object>();
    protected Map<String,Field> fieldMap;
    protected List<Field> fields;
    private BaseAnalysis() {

    }
    public BaseAnalysis(Class clazz) {
        this.clazz = clazz;
        this.init();
        this.setDataMap();
    }

    protected void init() {
        this.configuration = new Configuration(Configuration.VERSION_2_3_22);
        this.packageName = this.clazz.getPackage().getName();
        this.basePackagePath = "/com/halflife/framework/generator/template";

        this.appLocation = GenerateUtil.getAppLocation();
        this.srcLocation = this.appLocation + "/src/main/java";
        this.webLocation = this.appLocation + "/src/main/webapp/WEB-INF/app";


        dataMap.put("packageName", this.packageName);
        dataMap.put("objectName", this.clazz.getSimpleName().substring(0,1).toLowerCase()+this.clazz.getSimpleName().substring(1));
        dataMap.put("className", this.clazz.getSimpleName());
        QueryAnn queryAnn = (QueryAnn) this.clazz.getAnnotation(QueryAnn.class);
        dataMap.put("title", queryAnn.title());
        dataMap.put("mainTitle", queryAnn.mainTitle());
        fieldMap = new HashMap<>();
        fields = new ArrayList<>();
        GenerateUtil.setFieldsBySequence(clazz,fieldMap,fields);

    }
    public abstract void setDataMap();
    protected abstract String getTemplateFile();

    protected String getDefaultFileDir() {
        return this.srcLocation+"/"+this.packageName.replaceAll("\\.", "/")+"/";
    }

    protected abstract String getFileDir();

    protected abstract String getFileName();
    public void handler(){
        //要填入模本的数据文件
        //设置模本装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet，classpath，数据库装载，
        //这里我们的模板是放在com.havenliu.document.template包下面
        configuration.setClassForTemplateLoading(this.getClass(), this.basePackagePath);
        Template t=null;
        try {
            //test.ftl为要装载的模板
            t = configuration.getTemplate(this.getTemplateFile());
        } catch (IOException e) {
            e.printStackTrace();
        }
        //输出文档路径及名称
        Writer out = null;
        try {
            File dir = new File(this.getFileDir());
            GenerateUtil.mkDir(dir);
            //Writer w = new OutputStreamWriter(System.out);
            Writer w = new OutputStreamWriter(new FileOutputStream(this.getFileDir()+this.getFileName()));
            out = new BufferedWriter(w);
            w.flush();
        } catch (Exception e1) {
            e1.printStackTrace();
        }

        try {
            t.process(dataMap, out);
            out.close();
        } catch (TemplateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
